/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.send.policy;

import cn.net.greenet.common.db_service.BaseDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 通过策略库ID获取与之对应的设备实例的ID和Name
 *
 * @author liangdelin
 */
public class GetDeviceUntil {

    public static String getDevice(String id) throws SQLException {
        List<Integer> deviceId = new ArrayList<>();  //存放与之对应的设备ID集合
        Map<Integer, String> data = new HashMap<>(); //最终获取的结果数据
        BaseDao baseDao = new BaseDao();
        ResultSet rs = baseDao.executeSQL("select DPIEndPointBean_id from DPIEndPointBean_has_PolicyRepositoryInstance where PolicyRepositoryInstance_id = ?", new Object[]{id});
        while (rs.next()) {
            deviceId.add(rs.getInt("DPIEndPointBean_id"));
        }
        for (int i = 0; i < deviceId.size(); i++) {
            ResultSet dpirs = baseDao.executeSQL("select name from DPIEndPointBean where id = ?", new Object[]{deviceId.get(i)});
            while (dpirs.next()) {
                data.put(deviceId.get(i), dpirs.getString("name"));
            }
        }
        JSONArray jsonArr = new JSONArray();
        for (int j = 0; j < deviceId.size(); j++) {
            JSONObject jo = new JSONObject();
            jo.put("deviceId", deviceId.get(j));
            jo.put("deviceName", data.get(deviceId.get(j)));
            jsonArr.add(jo);
        }
        return jsonArr.toString();
    }
}
